<?php
session_start();
 if (empty($_SESSION['username']) AND empty($_SESSION['passuser'])){
  echo "<link href='style.css' rel='stylesheet' type='text/css'>
 <center>Untuk mengakses modul, Anda harus login <br>";
  echo "<a href=../../index.php><b>LOGIN</b></a></center>";
}
else{
$aksi="modul/mod_produk/aksi_produk.php";
switch($_GET[act]){
  // Tampil Produk
  default:
  ?>
<div id="toolbar-box">
	<div class="t">
		<div class="t">
			<div class="t"></div>
		</div>
	</div>
	<div class="m">
		<div class="toolbar" id="toolbar">
			<table class="toolbar">
            	<tr>
					<td class="button" id="toolbar-new">
  					<a href='?module=produk&act=tambahproduk' onclick="javascript:hideMainMenu(); submitbutton('add')" class="toolbar">
  					<span class="icon-32-new" title="New">
  					</span>Tambah Data Produk
  					</a>
					</td>
				</tr>
             </table>
		</div>
		<div class="header icon-48-addedit">Data Produk</div>
			<div class="clr"></div>
			</div>
		<div class="b">
			<div class="b">
				<div class="b"></div>
			</div>
		</div>
</div>
  
 <? 
  
    echo "<table>
          <tr><th width=30px>no</th><th>nama produk</th><th>berat(ons)</th><th>harga</th><th>diskon(%)</th><th>stok</th><th>tgl. masuk</th><th>aksi</th></tr>";

    $p      = new Paging;
    $batas  = 10;
    $posisi = $p->cariPosisi($batas);

    $tampil = mysql_query("SELECT * FROM produk ORDER BY id_produk DESC LIMIT $posisi,$batas");
  
    $no = $posisi+1;
    while($r=mysql_fetch_array($tampil)){
      $tanggal=tgl_indo($r[tgl_masuk]);
      $harga=format_rupiah($r[harga]);
      echo "<tr align=center><td>$no</td>
                <td align=left>$r[nama_produk]</td>
                <td align=center>$r[berat]</td>
                <td>$harga</td>
                <td align=center>$r[diskon]</td>
                <td align=center>$r[stok]</td>
                <td>$tanggal</td>
		            <td align=center width=120px><a class='tombol' href=?module=produk&act=editproduk&id=$r[id_produk]>Edit</a>
		                <a href='$aksi?module=produk&act=hapus&id=$r[id_produk]&namafile=$r[gambar]' class='tombol' onClick='return hapus()'>Hapus</a></td>
		        </tr>";
      $no++;
    }
    echo "</table>";

    $jmldata = mysql_num_rows(mysql_query("SELECT * FROM produk"));
    $jmlhalaman  = $p->jumlahHalaman($jmldata, $batas);
    $linkHalaman = $p->navHalaman($_GET[halaman], $jmlhalaman);

    echo "<div id=paging>Hal: $linkHalaman</div><br>";
 
    break;
  
  case "tambahproduk":
  
 ?>
<div id="toolbar-box">
	<div class="t">
		<div class="t">
			<div class="t"></div>
		</div>
	</div>
	<div class="m">
		<div class="toolbar" id="toolbar">
			<table class="toolbar">
            	<tr>
					<td class="button" id="toolbar-new">
  					<a href=?module=produk onclick="javascript: submitbutton('cancel')" class="toolbar"> <span class="icon-32-cancel" title="Cancel"></span>
Cancel</a></td>
  					<span class="icon-32-new" title="New">
  					</span>
  					</a>
					</td>
				</tr>
             </table>
		</div>
		<div class="header icon-48-addedit">Tambah Data Produk</div>
			<div class="clr"></div>
			</div>
		<div class="b">
			<div class="b">
				<div class="b"></div>
			</div>
		</div>
</div>

<script type="text/javascript" src="js/jquery-1.2.3.pack.js"></script>
<script type="text/javascript" src="js/jquery.validate.pack.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	$("#Form").validate({
		messages: {
			email: {
				required: "E-mail harus diisi",
				email: "Masukkan E-mail yang valid"
			}
		},
		errorPlacement: function(error, element) {
			error.appendTo(element.parent("td"));
		}
	});
})
</script>
<style type="text/css">
input.error, select.error { 
	border: 2px solid red;
	-moz-box-shadow:0 0 10px rgba(255, 0, 0, 0.6);
	-webkit-box-shadow:0 0 10px rgba(255, 0, 0, 0.6);
	}
label.error { color:red; margin-left: 10px; }
</style>
 <?
  
    echo "<form method=POST action='$aksi?module=produk&act=input' enctype='multipart/form-data' id='Form'>
          <table>
          <tr><td width=100>Nama Produk</td>     <td> : <input type=text name='nama_produk' size=60 class='required' title='&curren; Nama Produk Harus diisi'></td></tr>
          <tr><td>Kategori</td>  <td> : 
          <select name='kategori' class='required' title='&curren; Kategori Produk Harus diisi'>
            <option value='' selected>- Pilih Kategori -</option>";
            $tampil=mysql_query("SELECT * FROM kategori_produk ORDER BY nama_kategori");
            while($r=mysql_fetch_array($tampil)){
              echo "<option value=$r[id_kategori]>$r[nama_kategori]</option>";
            }
    echo "</select></td></tr>
          <tr><td>Berat</td>     <td> : <input type=text name='berat' size=3 class='required' title='&curren; Berat Produk Harus diisi'> ONS</td></tr>
          <tr><td>Harga</td>     <td> : <input type=text name='harga' size=10 class='required' title='&curren; Harga Produk Harus diisi'></td></tr>
          <tr><td>Diskon</td>     <td> : <input type=text name='diskon' size=2> %</td></tr>
          <tr><td>Stok</td>     <td> : <input type=text name='stok' size=3 class='required' title='&curren; Stok Produk Harus diisi'></td></tr>
          <tr><td>Deskripsi</td>  <td> <textarea name='deskripsi' style='min-width:700px; height: 350px;' class='required' title='&curren; Deskripsi Produk Harus diisi'></textarea></td></tr>
          <tr><td>Gambar</td>      <td> : <input type=file name='fupload' size=40> 
                                          <br>Tipe gambar harus JPG/JPEG dan ukuran lebar maks: 400 px</td></tr>
          <tr><td colspan=2><input type=submit value=Simpan class=tombol1>
                            <input type=button value=Batal onclick=self.history.back() class=tombol1></td></tr>
          </table></form>";
     break;
    
  case "editproduk":
  ?>
<div id="toolbar-box">
	<div class="t">
		<div class="t">
			<div class="t"></div>
		</div>
	</div>
	<div class="m">
		<div class="toolbar" id="toolbar">
			<table class="toolbar">
            	<tr>
					<td class="button" id="toolbar-new">
  					<a href=?module=produk onclick="javascript: submitbutton('cancel')" class="toolbar"> <span class="icon-32-cancel" title="Cancel"></span>
Cancel</a></td>
  					<span class="icon-32-new" title="New">
  					</span>
  					</a>
					</td>
				</tr>
             </table>
		</div>
		<div class="header icon-48-addedit">Edit Data Produk</div>
			<div class="clr"></div>
			</div>
		<div class="b">
			<div class="b">
				<div class="b"></div>
			</div>
		</div>
</div>

<script type="text/javascript" src="js/jquery-1.2.3.pack.js"></script>
<script type="text/javascript" src="js/jquery.validate.pack.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	$("#Form").validate({
		messages: {
			email: {
				required: "E-mail harus diisi",
				email: "Masukkan E-mail yang valid"
			}
		},
		errorPlacement: function(error, element) {
			error.appendTo(element.parent("td"));
		}
	});
})
</script>
<style type="text/css">
input.error, select.error { 
	border: 2px solid red;
	-moz-box-shadow:0 0 10px rgba(255, 0, 0, 0.6);
	-webkit-box-shadow:0 0 10px rgba(255, 0, 0, 0.6);
	}
label.error { color:red; margin-left: 10px; }
</style>
 <?
  
  
  
    $edit = mysql_query("SELECT * FROM produk WHERE id_produk='$_GET[id]'");
    $r    = mysql_fetch_array($edit);

    echo "<form method=POST enctype='multipart/form-data' action=$aksi?module=produk&act=update id='Form'>
          <input type=hidden name=id value=$r[id_produk]>
          <table>
          <tr><td width=70>Nama Produk</td>     <td> : <input type=text name='nama_produk' size=60 value='$r[nama_produk]' class='required' title='&curren; Nama Produk Harus diisi'></td></tr>
          <tr><td>Kategori</td>  <td> : <select name='kategori' class='required' title='&curren; Kategori Produk Harus diisi'>";
 
          $tampil=mysql_query("SELECT * FROM kategori_produk ORDER BY nama_kategori");
          if ($r[id_kategori]==''){
            echo "<option value=0 selected>- Pilih Kategori -</option>";
          }   

          while($w=mysql_fetch_array($tampil)){
            if ($r[id_kategori]==$w[id_kategori]){
              echo "<option value=$w[id_kategori] selected>$w[nama_kategori]</option>";
            }
            else{
              echo "<option value=$w[id_kategori]>$w[nama_kategori]</option>";
            }
          }
    echo "</select></td></tr>
          <tr><td>Berat</td>     <td> : <input type=text name='berat' value=$r[berat] size=3 class='required' title='&curren; Berat Produk Harus diisi'></td></tr>
          <tr><td>Harga</td>     <td> : <input type=text name='harga' value=$r[harga] size=10 class='required' title='&curren; Harga Produk Harus diisi'></td></tr>
          <tr><td>Diskon</td>     <td> : <input type=text name='diskon' value=$r[diskon] size=2></td></tr>
          <tr><td>Stok</td>     <td> : <input type=text name='stok' value=$r[stok] size=3 class='required' title='&curren; Stok Produk Harus diisi'></td></tr>
          <tr><td>Deskripsi</td>   <td> <textarea name='deskripsi' style='min-width:700px; height: 350px;' class='required' title='&curren; Deskripsi Produk Harus diisi'>$r[deskripsi]</textarea></td></tr>
          <tr><td>Gambar</td>       <td> :  
          <img src='../foto_produk/small_$r[gambar]'></td></tr>
          <tr><td>Ganti Gbr</td>    <td> : <input type=file name='fupload' size=30> *)</td></tr>
          <tr><td colspan=2>*) Apabila gambar tidak diubah, dikosongkan saja.</td></tr>
          <tr><td colspan=2><input type=submit value=Update class=tombol1>
                            <input type=button value=Batal onclick=self.history.back() class=tombol1></td></tr>
         </table></form>";
    break;  
}
}
?>
